package com.bugreport.web;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.unitils.UnitilsJUnit4;

import com.bugreport.utils.BugReportException;

public class BRJSControllerTestCase extends UnitilsJUnit4 {

	private BugReportJSController controller = new BugReportJSController();
	private final static String BUG_REPORT_JS_URL = "/ressources/bugreport.js";

	@Test
	public void refusePOSTRequest() {

		// Given
		MockHttpServletRequest request = new MockHttpServletRequest("POST", BUG_REPORT_JS_URL);
		HttpServletResponse response = new MockHttpServletResponse();

		try {

			// When
			controller.handleRequest(request, response);
			fail("Expected exception");

		} catch (Exception ex) {

			// Then
			assertNotNull(ex);
			assertTrue(ex instanceof BugReportException);
			assertEquals(500, ((BugReportException) ex).getCode());
		}
	}

	@Test
	public void getJSWithJQuery() throws Exception {

		// Given
		MockHttpServletRequest request = new MockHttpServletRequest("GET", BUG_REPORT_JS_URL);
		request.setParameter(BugReportJSController.PARAM_INCLUDE_JQUERY, "true");

		HttpServletResponse response = new MockHttpServletResponse();

		// When
		ModelAndView mav = controller.handleRequest(request, response);

		// Then
		assertNotNull(mav);
		assertEquals(BugReportJSController.SUCCESS_VIEW, mav.getViewName());
		assertTrue((Boolean) mav.getModel().get(BugReportJSController.SUCCESS_VIEW_INCLUDE_JQUERY));
	}

	@Test
	public void getJSWithJQueryWithLowerCaseParameter() throws Exception {

		// Given
		MockHttpServletRequest request = new MockHttpServletRequest("GET", BUG_REPORT_JS_URL);
		request.setParameter(BugReportJSController.PARAM_INCLUDE_JQUERY.toLowerCase(), "true");

		HttpServletResponse response = new MockHttpServletResponse();

		// When
		ModelAndView mav = controller.handleRequest(request, response);

		// Then
		assertNotNull(mav);
		assertEquals(BugReportJSController.SUCCESS_VIEW, mav.getViewName());
		assertTrue((Boolean) mav.getModel().get(BugReportJSController.SUCCESS_VIEW_INCLUDE_JQUERY));
	}

	@Test
	public void getJSWithJQueryByDefault() throws Exception {

		// Given
		MockHttpServletRequest request = new MockHttpServletRequest("GET", BUG_REPORT_JS_URL);
		HttpServletResponse response = new MockHttpServletResponse();

		// When
		ModelAndView mav = controller.handleRequest(request, response);

		// Then
		assertNotNull(mav);
		assertEquals(BugReportJSController.SUCCESS_VIEW, mav.getViewName());
		assertTrue((Boolean) mav.getModel().get(BugReportJSController.SUCCESS_VIEW_INCLUDE_JQUERY));
	}

	@Test
	public void getJSWithoutJQuery() throws Exception {

		// Given
		MockHttpServletRequest request = new MockHttpServletRequest("GET", BUG_REPORT_JS_URL);
		request.setParameter(BugReportJSController.PARAM_INCLUDE_JQUERY, "false");

		HttpServletResponse response = new MockHttpServletResponse();

		// When
		ModelAndView mav = controller.handleRequest(request, response);

		// Then
		assertNotNull(mav);
		assertEquals(BugReportJSController.SUCCESS_VIEW, mav.getViewName());
		assertFalse((Boolean) mav.getModel().get(BugReportJSController.SUCCESS_VIEW_INCLUDE_JQUERY));
	}

	@Test
	public void correctApplicationPath() throws Exception {

		// Given
		MockHttpServletRequest request = new MockHttpServletRequest("GET", BUG_REPORT_JS_URL);
		HttpServletResponse response = new MockHttpServletResponse();

		// When
		ModelAndView mav = controller.handleRequest(request, response);

		// Then
		assertNotNull(mav);
		assertEquals(BugReportJSController.SUCCESS_VIEW, mav.getViewName());

		String applicationPath = (String) mav.getModel().get(BugReportJSController.SUCCESS_VIEW_APPLICATION_PATH);

		assertNotNull(applicationPath);
		assertEquals("http://localhost:80", applicationPath);
	}
}
